<?php
namespace Admin\Model;
use Think\Model;
class AdminModel extends Model {
	protected $tableName = 'admin'; 
	protected $_validate = array(
		array('username','require','用户名不能为空！'),
		array('password','require','密码不能为空！',2), 
	);

	/**
	 * 判断是否是管理员
	 */
	public function checkadmin() {
		$where['username'] = I('post.username','','strip_tags');
		$where['password'] = I('post.password','','md5');
		$info = $this->where($where)->find();
		return $info;
	}

	/**
	 * 修改密码
	 */
	public function editpassword() {
		$data['password'] = I('post.password','','md5');
		$msg = $this->where(array('id'=>$_SESSION['admin']['id']))->save($data);
		return $msg;
	}

	/**
	 * 获取管理员列表
	 */
	public function mselect() {
		$where['id'] = array('neq',$_SESSION['admin']['id']);
		$list = $this->where($where)->select();
		return $list;
	}

	/**
	 * 添加管理员
	 */
	public function msave() {
		$data['username'] = I('post.username','','strip_tags');
		$where['id'] = I('post.id','','intval');
		if(empty($where['id'])) { //判断是添加还是修改
			$data['password'] = I('post.password','','md5');
			$msg = $this->add($data);
		} else {
			if(!empty($_POST['password'])) {
				$data['password'] = I('post.password','','md5');
			}
			$msg = $this->where($where)->save($data);
		}
		return $msg;
	}

}
